//绩效评分模块
import axios from 'axios'

const $host = 'http://132.232.110.185/personnelManagement'

export default {
    namespaced: true,
    actions: {
        //ajax请求评分页数据
        async getRequestData(ctx, {page, limit}) {
            await axios.get(`/PerformanceScore/ShowEmployeeScore/${page}/${limit}`)
                .then(res => {
                    ctx.commit('saveData', res.data)
                }).catch(err => {
                    console.log(err)
                })
        },
        //请求评分记录页
        async getScoreRecords(ctx, {page, limit, massage = ''}) {
            await axios.get(`${$host}/PerformanceScore/ScoreRecord/${page}/${limit}/${massage}`)
                .then(res => {
                    ctx.commit("saveScoreRecords", res.data)
                }).catch(err => {
                    console.log(err)
                })
        },
        //修改请求
        async updateScore(ctx, {id, grade}) {
            await axios.put(`${$host}/PerformanceScore/updateScore/${id}/${grade}`)
                .then(res => {
                    if (res.data.code === 100) ctx.commit("UPDATESCORE")
                }).catch(err => {
                    console.log(err)
                })
        },
        async updateScorePost(ctx, {id, grade}) {
            await axios.post(`${$host}/PerformanceScore/AddScore/${id}/${grade}`)
                .then(res => {
                    if (res.data.code === 100) ctx.commit("UPDATESCOREPOST")
                }).catch(err => {
                    console.log(err)
                })
        },
        //多条件查询
        async searchSomething(ctx, obj) {
            await axios({
                url: `${$host}/PerformanceScore/ConditionQuery`,
                method: 'GET',
                params: {
                    id: obj.id || null,
                    name: obj.name || null,
                    sex: obj.sex || null,
                }
            }).then(res => {
                ctx.commit("searchCnt", res.data)
            }).catch(err => {
                console.log(err)
            })

        },
        //记录搜索
        async recordSearch(ctx, obj) {
            let fd = new FormData()
            fd.append('message', obj.id || obj.name || obj.sex)
            await axios({
                method: 'GET',
                url: `${$host}/PerformanceScore/ScoreRecord/1/10`,
                params: {
                    message: obj.id || obj.name || obj.sex
                }
            }).then(res => {
                console.log(res)
                ctx.commit("recordCnt", res.data)
            }).catch(err => {
                console.log(err)
            })
        }
    },
    mutations: {
        //保存请求的数据
        saveData(state, res) {
            state.tableData = res.data
            state.total1 = res.count
        },
        saveScoreRecords(state, res) {
            state.scoreRecord = res.data
            state.total2 = res.count
        },
        //弹出修改得分模态框
        showScoreDialog(state, row) {
            state.updateStatus = false
            state.dialogStatus.scoreFormVisible = true
            state.preScore = row.sgrade
            state.scoreForm = row
        },
        //选择部门
        changeDepartment(state, val) {
            state.scoreForm.dname = val
        },
        //保存修改的分数
        UPDATESCORE(state) {
            state.updateStatus = true
            state.dialogStatus.scoreFormVisible = false
        },
        UPDATESCOREPOST(state) {
            state.updateStatus = true
            state.dialogStatus.scoreFormVisible = false
        },
        //显示搜索框
        showSearchDialog(state) {
            state.dialogStatus.searchVisible = true
            state.searchForm = {}
        },
        closeDialog(state) {
            state.dialogStatus.scoreFormVisible = false
        },
        //搜索
        searchCnt(state, res) {
            state.tableData = res.data
            state.total1 = res.count
            state.dialogStatus.searchVisible = false
        },
        recordCnt(state, res) {
            state.scoreRecord = res.data
            state.total2 = res.count
            state.dialogStatus.searchVisible = false
        }
    },
    state: {
        preScore: '',
        tableData: [],
        updateStatus: false,
        department: [],
        page: 1,
        limit: 10,
        total1: '',
        total2: '',
        scoreRecord: [],
        scoreForm: {},
        dialogStatus: {scoreFormVisible: false, searchVisible: false},
        searchForm: {
            id: '',
            name: '',
            sex: ''
        },
        searchForm1: {
            id: '',
            name: '',
            sex: ''
        }
    },
    getters: {}
}